package me.spring.cloud.common.components.rabbitmq.configuration.dlx;

import me.spring.cloud.common.components.rabbitmq.configuration.simple.AbstractSimpleQueueConfiguration;
import me.spring.cloud.common.components.rabbitmq.util.RabbitMqUtil;
import java.util.HashMap;
import java.util.Map;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;

/**
 * @author luffy
 * @description: 抽象实现延时队列
 * @date 2019-05-24 16:35
 */
public abstract class AbstractDLXQueueConfiguration extends AbstractSimpleQueueConfiguration implements
    DLXQueueConfiguration {

  /**
   * @Return org.springframework.amqp.core.Queue
   * @Author luffy
   * @Description 创建延迟队列
   * @Date 2019-05-24 18:12
   * @Param []
   **/
  protected Queue createDeadLetterQueue() {
    Map<String, Object> arguments = new HashMap<>(2);
    arguments.put("x-dead-letter-exchange", defineExchangeName());
    arguments.put("x-dead-letter-routing-key", defineQueueName());
    return new Queue(defineDeadLetterQueue(), true, false, false, arguments);
  }

  /**
   * @Return java.lang.String
   * @Author luffy
   * @Description 死信队列
   * @Date 2019-05-27 16:20
   * @Param []
   **/
  private String defineDeadLetterQueue() {
    return RabbitMqUtil.getDeadQueueName(defineQueueName());
  }

  /**
   * @Return org.springframework.amqp.core.Binding
   * @Author luffy
   * @Description 绑定死信队列
   * @Date 2019-05-24 18:15
   * @Param []
   **/
  protected Binding createDeadLetterBinding() {
    return BindingBuilder.bind(createDeadLetterQueueBean()).to(creeateExchangeBean()).with(defineDeadLetterQueue());
  }
}
